
class_name CPlayerSceneArray
extends EventDispatcher

signal UUID_changed(old_value, new_value) 
signal MaskID_changed(old_value, new_value) 
signal SceneID_changed(old_value, new_value) 
signal Location_changed(old_value, new_value) 
signal TransPoint_changed(old_value, new_value) 



class SPlayerScene:
    var nIndex
    var tblOwner
    var m_tblAlias
    var bInited = false
    func _init(owner):
        self.UUID = 0
        self.MaskID = owner and owner.m_UUID or 0
        self.SceneID = 0
        self.Location = ''
        self.TransPoint = ''

        self.nIndex = -1
        self.tblOwner = owner
        self.m_tblAlias = {
 
        }

    func GetIndex():
        return self.nIndex

    func SetZeroValue(owner):
        self.UUID = 0
        self.MaskID = owner and owner.m_UUID or 0
        self.SceneID = 0
        self.Location = ''
        self.TransPoint = ''


    func UnPack(_bufferStream, _bOffset):
        if _bOffset :
            _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)

        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.SceneID = _bufferStream.ReadUint32()
        self.Location = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_Location)
        self.TransPoint = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_TransPoint)

        self.bInited = true
        if self.tblOwner :
            self.tblOwner.DispatchEvent('uuid', [self, 0, self.nIndex])

    func CopyFrom(other):
        self.nIndex = other.nIndex
        self.SceneID = other.SceneID
        self.Location = other.Location
        self.TransPoint = other.TransPoint


    func Update(_bufferStream, _nIndex ):
        _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)
        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.SceneID = _bufferStream.ReadUint32()
        var newLocation = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_Location)
        if newLocation != self.m_Location :
            var oldLocation = self.m_Location
            self.m_Location = newLocation
            if self.tblOwner :
                self.tblOwner.DispatchEvent('Location', [self, oldLocation, newLocation])
            
        
        var newTransPoint = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_TransPoint)
        if newTransPoint != self.m_TransPoint :
            var oldTransPoint = self.m_TransPoint
            self.m_TransPoint = newTransPoint
            if self.tblOwner :
                self.tblOwner.DispatchEvent('TransPoint', [self, oldTransPoint, newTransPoint])
            
        


    func OnDataChanged( fieldName, oldValue, newValue ):
        if oldValue == newValue or not self.tblOwner:
            return
        self.tblOwner.emit_signal(fieldName, oldValue, newValue)


    #  SPlayerScene Geter Setter method # 
    var UUID:
        set(_value):
            var old = self.m_UUID
            self.UUID = _value
            if self.tblOwner:
                self.tblOwner.UUID_changed(old, _value)
        get:
            return UUID

    var MaskID:
        set(_value):
            var old = self.m_MaskID
            self.MaskID = _value
            if self.tblOwner:
                self.tblOwner.MaskID_changed(old, _value)
        get:
            return MaskID

    var SceneID:
        set(_value):
            var old = self.m_SceneID
            self.SceneID = _value
            if self.tblOwner:
                self.tblOwner.SceneID_changed(old, _value)
        get:
            return SceneID

    var Location:
        set(_value):
            var old = self.m_Location
            self.Location = _value
            if self.tblOwner:
                self.tblOwner.Location_changed(old, _value)
        get:
            return Location

    var TransPoint:
        set(_value):
            var old = self.m_TransPoint
            self.TransPoint = _value
            if self.tblOwner:
                self.tblOwner.TransPoint_changed(old, _value)
        get:
            return TransPoint



# ================================= array ==========================
var m_UUID
var m_nPlayerSceneCount
var m_PlayerScene

signal elem_update(array, old_value, new_value, index)
signal elem_add(array, old_value, new_value, index)
signal elem_del(array, old_value, new_value, index)

func _init( _uuid ):
    self.m_UUID = _uuid
    self.m_nPlayerSceneCount = 0
    self.m_PlayerScene = []
    var nMaxIndex = self.GetPlayerSceneMaxCount()-1
    self.m_PlayerScene.resize(nMaxIndex)
    for i in range(nMaxIndex):
        self.m_PlayerScene[i] = SPlayerScene.new(self) 
        self.m_PlayerScene[i].nIndex = i

func UnPack(_bufferStream):
    self.m_UUID = _bufferStream.ReadInt64()
    self.m_nPlayerSceneCount = _bufferStream.ReadUint32()
    for i in range(self.m_nPlayerSceneCount):
        var elem = SPlayerScene.new(self)
        elem.nIndex = i
        elem.UnPack(_bufferStream, false)
        self.m_PlayerScene[i] = elem

func GetTableName():
    return 'PlayerScene'

func GetUUID():
    return self.m_UUID

func GetPlayerSceneCurNum():
    return self.m_nPlayerSceneCount

func SetPlayerSceneCurNum( nValue ):
    self.m_nPlayerSceneCount = nValue

func SetCurNum(nValue):
    self.m_nPlayerSceneCount = nValue

static func GetPlayerSceneMaxCount():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSCENE

func GetPlayerSceneMaxNum():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSCENE

func GetPlayerScene(_nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSCENE:
        print("GetPlayerSceneEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSCENE")
        return null

    return self.m_PlayerScene[_nIndex];

func SetPlayerScene(_Prop, _nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSCENE:
        print("SetPlayerSceneEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSCENE")
        return null
    
    var old = self.m_PlayerScene[_nIndex]
    var elem = SPlayerScene.new(self)
    elem.CopyFrom(_Prop)
    elem.nIndex = _nIndex
    self.m_PlayerScene[_nIndex] = elem
    elem_update.emit(self, old, elem, _nIndex)
    return elem


func AddPlayerScene(_Prop, _nIndex):
    var nCurNum = self.m_nPlayerSceneCount
    if _nIndex < 0 or _nIndex > nCurNum:
        _nIndex = nCurNum

    if not _Prop or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSCENE:
        print("AddPlayerSceneEle _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSCENE");
        return null;

    var elem = SPlayerScene.new(self)
    elem.CopyFrom(_Prop)
    if _nIndex < nCurNum:
        for i in range(nCurNum-1, -1, -1):
            self.m_PlayerScene[i] = self.m_PlayerScene[i - 1]
            self.m_PlayerScene[i].nIndex = i
    self.m_PlayerScene[_nIndex] = elem
    self.m_nPlayerSceneCount = nCurNum + 1
    elem_add.emit(self, null, elem, _nIndex)

func DelPlayerScene(_nIndex):
    if _nIndex < 0 or _nIndex >= self.m_nPlayerSceneCount:
        return
    
    self.m_nPlayerSceneCount = self.m_nPlayerSceneCount - 1
    if _nIndex != self.m_nPlayerSceneCount:
        self.m_PlayerScene[_nIndex] = self.m_PlayerScene[self.m_nPlayerSceneCount]

    # var nLastDataID = self.m_PlayerScene[self.m_nPlayerSceneCount]
    var elem = self.m_PlayerScene[_nIndex]
    for i in range(_nIndex + 1, self.m_nPlayerSceneCount - 1):
        self.m_PlayerScene[i - 1] = self.m_PlayerScene[i]
        self.m_PlayerScene[i - 1].nIndex = i - 1
    self.m_PlayerScene[self.m_nPlayerSceneCount - 1] = null
    elem_del.emit(self, elem, null, _nIndex)


func CopyFrom(_pOther):
    self.m_nPlayerSceneCount = _pOther.m_nPlayerSceneCount
    for i in range(_pOther.m_nPlayerSceneCount):
        self.m_PlayerScene[i] = SPlayerScene.new(self)
        self.m_PlayerScene[i].CopyFrom(_pOther.m_PlayerScene[i])

func UnPackField(_fieldId, _bufferStream, _varLen):
    var elemCount = self.m_nPlayerSceneCount
    if _fieldId < 0 or _fieldId > TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSCENE:
        # var szLog = string.format("CPlayerSceneArray.UnPackField Error :\
        #  _fieldId(%d) < 0 or _fieldId(%d) > self.MaxPlayerSceneEleCount()(%d)",
        #  _fieldId, _fieldId, TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERSCENE)
        return false
    var elem = null
    var old = null
    if self.m_PlayerScene[_fieldId] != null:
        elem = self.m_PlayerScene[_fieldId]
        old = SPlayerScene.new( self )
        old.CopyFrom( elem )
    else:
        elem = SPlayerScene.new(self)
        self.m_PlayerScene[_fieldId] = elem
        self.m_PlayerScene[_fieldId].nIndex = _fieldId

    if elemCount <= _fieldId:
        self.m_nPlayerSceneCount = _fieldId + 1

    elem.Update(_bufferStream, _fieldId )
    elem_update.emit(self, old, elem, _nIndex)
    return true

func AddField(_fieldId, _bufferStream):
    var elem = SPlayerScene.new(self)
    elem.UnPack(_bufferStream, true )
    elem.nIndex = _fieldId
    if null == self.m_PlayerScene[_fieldId]:
        self.m_PlayerScene[_fieldId] = elem
    else:
        var uuid = elem.m_UUID;
        for index in range(self.m_nPlayerSceneCount, _fieldId+1, -1):
            self.m_PlayerScene[index] = self.m_PlayerScene[index- 1]
            self.m_PlayerScene[index].m_UUID = uuid + index - _fieldId
            self.m_PlayerScene[index].nIndex = index
        self.m_PlayerScene[_fieldId] = elem;
        self.m_PlayerScene[_fieldId].nIndex = _fieldId

    self.m_nPlayerSceneCount = self.m_nPlayerSceneCount + 1;
    elem_add.emit(self, null, elem, _fieldId)

func DelField(_fieldId ):
    if self.m_PlayerScene[_fieldId] == null:
        return 
    
    self.m_nPlayerSceneCount = self.m_nPlayerSceneCount - 1
    var elem = self.m_PlayerScene[_fieldId]
    if _fieldId != self.m_nPlayerSceneCount:
        var uuid = self.m_PlayerScene[_fieldId].m_UUID;
        self.m_PlayerScene[_fieldId] = self.m_PlayerScene[self.m_nPlayerSceneCount]
        self.m_PlayerScene[_fieldId].m_UUID = uuid
        self.m_PlayerScene[_fieldId].nIndex = _fieldId

    self.m_PlayerScene[self.m_nPlayerSceneCount] = null
    elem_del.emit(self, elem, null, _fieldId)   

func Filter( funFilter ):
    var tblItems = []
    for i in range(self.m_nPlayerSceneCount-1):
        if funFilter.call(self.m_PlayerScene[i], i):
            tblItems.append(self.m_PlayerScene[i])
    return tblItems

func QueryBySceneID(SceneID):
    var nCnt = self.m_nPlayerSceneCount
    for i in range(nCnt-1):
        var curElem = self.m_PlayerScene[i]
        if curElem != null :
            if curElem.m_MaskID > 0 and curElem.m_SceneID == SceneID :
                return curElem
    return null


